<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="任务类别" prop="taskType">
        <el-select v-model="queryParams.taskType" placeholder="请选择任务类别" clearable>
          <el-option
            v-for="dict in dict.type.sys_inspection_task_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="任务状态" prop="taskStatusId">
        <el-select v-model="queryParams.taskStatusId" placeholder="请选择任务状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_inspection_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="评价" prop="evaluation">
        <el-select v-model="queryParams.evaluation" placeholder="请选择评价" clearable>
          <el-option
            v-for="dict in dict.type.sys_inspection_evaluation"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
	    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:inspection:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-sort"
          size="mini"
          @click="toggleExpandAll"
        >展开/折叠</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="inspectionList"
      row-key="methodId"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="措施编号" prop="methodId" />
      <el-table-column label="任务类别" align="center" prop="taskType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_inspection_task_type" :value="scope.row.taskType"/>
        </template>
      </el-table-column>
      <el-table-column label="整改措施" align="center" prop="method" />
      <el-table-column label="完成时限" align="center" prop="deadLine" />
      <el-table-column label="单位名称" align="center" prop="unitName" />
      <el-table-column label="部门名称" align="center" prop="deptName" />
      <el-table-column label="本单位措施编号" align="center" prop="unitMethodId" />
      <el-table-column label="任务状态" align="center" prop="taskStatusId" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_inspection_status" :value="scope.row.taskStatusId"/>
        </template>
      </el-table-column>
      <el-table-column label="上级措施编号" align="center" prop="parentMethodId" />
      <el-table-column label="评价" align="center" prop="evaluation">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_inspection_evaluation" :value="scope.row.evaluation"/>
        </template>
      </el-table-column>
      <el-table-column label="评价时间" align="center" prop="evaluationDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.evaluationDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

      <el-table-column label="评价" align="center" class-name="small-padding fixed-width" width="150px">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="success"
            icon="el-icon-success"
            @click="handleGreen(scope.row)"
            v-hasPermi="['system:inspection:edit']"
            :disabled="scope.row.evaluation =='0'"
          >绿灯</el-button>
          <el-button
            size="mini"
            type="warning"
            icon="el-icon-warning"
            @click="handleYellow(scope.row)"
            v-hasPermi="['system:inspection:edit']"
            :disabled="scope.row.evaluation =='0'"
          >黄灯</el-button>
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-error"
            @click="handleRed(scope.row)"
            v-hasPermi="['system:inspection:edit']"
            :disabled="scope.row.evaluation =='0'"
          >红灯</el-button>
        </template>
      </el-table-column>
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150px">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:inspection:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['system:inspection:add']"
          >新增</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:inspection:remove']"
          >删除</el-button>
        </template>
      </el-table-column>-->
    </el-table>

    <!-- 添加或修改巡视巡查评价对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="措施编号" prop="methodId">
          <el-input v-model="form.methodId" placeholder="请输入措施编号" />
        </el-form-item>
        <el-form-item label="任务类别" prop="taskType">
          <el-select v-model="form.taskType" placeholder="请选择任务类别">
            <el-option
              v-for="dict in dict.type.sys_inspection_task_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="整改措施" prop="method">
          <el-input v-model="form.method" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="完成时限" prop="deadLine">
          <el-input v-model="form.deadLine" placeholder="请输入完成时限" />
        </el-form-item>
        <el-form-item label="单位名称" prop="unitName">
          <el-input v-model="form.unitName" placeholder="请输入单位名称" />
        </el-form-item>
        <el-form-item label="部门名称" prop="deptName">
          <el-input v-model="form.deptName" placeholder="请输入部门名称" />
        </el-form-item>
        <el-form-item label="任务状态" prop="taskStatusId">
          <el-select v-model="form.taskStatusId" placeholder="请选择任务状态">
            <el-option
              v-for="dict in dict.type.sys_inspection_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="上级措施ID" prop="parentMethodId">
          <treeselect v-model="form.parentMethodId" :options="inspectionOptions" :normalizer="normalizer" placeholder="请选择上级措施ID" />
        </el-form-item>
        <el-form-item label="评价" prop="evaluation">
          <el-select v-model="form.evaluation" placeholder="请选择评价">
            <el-option
              v-for="dict in dict.type.sys_inspection_evaluation"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="评价时间" prop="evaluationDate">
          <el-date-picker clearable
            v-model="form.evaluationDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择评价时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新时间" prop="updateDate">
          <el-date-picker clearable
            v-model="form.updateDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择更新时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="本单位措施编号" prop="unitMethodId">
          <el-input v-model="form.unitMethodId" placeholder="请输入本单位措施编号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInspection, getInspection, delInspection, addInspection, updateInspection, inspectionEvaluation } from "@/api/system/inspection";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Inspection",
  dicts: ['sys_inspection_evaluation', 'sys_inspection_status', 'sys_inspection_task_type'],
  components: {
    Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 巡视巡查评价表格数据
      inspectionList: [],
      // 巡视巡查评价树选项
      inspectionOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        taskType: null,
        unitId: null,
        deptId: null,
        taskStatusId: null,
        evaluation: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询巡视巡查评价列表 */
    getList() {
      this.loading = true;
      listInspection(this.queryParams).then(response => {
        this.inspectionList = this.handleTree(response.data, "methodId", "parentMethodId");
        this.loading = false;
      });
    },
    /** 转换巡视巡查评价数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.methodId,
        label: node.methodId,
        children: node.children
      };
    },
	/** 查询巡视巡查评价下拉树结构 */
    getTreeselect() {
      listInspection().then(response => {
        this.inspectionOptions = [];
        const data = { methodId: 0, methodId: '顶级节点', children: [] };
        data.children = this.handleTree(response.data, "methodId", "parentMethodId");
        this.inspectionOptions.push(data);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        methodId: null,
        taskType: null,
        method: null,
        deadLine: null,
        unitId: null,
        unitName: null,
        deptId: null,
        deptName: null,
        taskStatusId: null,
        taskStatus: null,
        parentMethodId: null,
        evaluation: null,
        evaluationDate: null,
        updateDate: null,
        unitMethodId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.getTreeselect();
      if (row != null && row.methodId) {
        this.form.parentMethodId = row.methodId;
      } else {
        this.form.parentMethodId = 0;
      }
      this.open = true;
      this.title = "添加巡视巡查评价";
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      if (row != null) {
        this.form.parentMethodId = row.parentMethodId;
      }
      getInspection(row.id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改巡视巡查评价";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInspection(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInspection(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除巡视巡查评价编号为"' + row.id + '"的数据项？').then(function() {
        return delInspection(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 亮黄灯按钮操作 */
    handleYellow(row) {
      this.$modal.confirm('是否确认将措施编号为【"' + row.methodId + '"】的数据项亮【黄灯】？').then(function() {
        let data = {id:row.id, evaluation: 1}
        return inspectionEvaluation(data);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("亮灯成功");
      }).catch(() => {});
    },
    /** 亮红灯按钮操作 */
    handleRed(row) {
      this.$modal.confirm('是否确认将措施编号为【"' + row.methodId + '"】的数据项亮【红灯】？').then(function() {
        let data = {id:row.id, evaluation: 2}
        return inspectionEvaluation(data);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("亮灯成功");
      }).catch(() => {});
    },
    /** 亮绿灯按钮操作 */
    handleGreen(row) {
      this.$modal.confirm('是否确认将措施编号为【"' + row.methodId + '"】的数据项亮【绿灯】？').then(function() {
        let data = {id:row.id, evaluation: 0}
        return inspectionEvaluation(data);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("亮灯成功");
      }).catch(() => {});
    }
}
};
</script>
